package cn.cdutetc.library.service.impl;

import cn.cdutetc.library.entity.pojo.Book;
import cn.cdutetc.library.entity.pojo.BookCategory;
import cn.hutool.core.bean.BeanUtil;
import cn.cdutetc.library.entity.dto.BookCategoryDTO;
import cn.cdutetc.library.common.exception.BizException;
import cn.cdutetc.library.mapper.BookCategoryMapper;
import cn.cdutetc.library.mapper.BookMapper;
import cn.cdutetc.library.service.BookCategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 图书分类服务实现类
 */
@Service
public class BookCategoryServiceImpl extends ServiceImpl<BookCategoryMapper, BookCategory> implements BookCategoryService {

    @Autowired
    private BookMapper bookMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer createCategory(BookCategoryDTO categoryDTO) {
        // 检查分类名称是否存在
        LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BookCategory::getCategoryName, categoryDTO.getCategoryName());
        if (this.count(queryWrapper) > 0) {
            throw new BizException("分类名称已存在");
        }
        
        // 创建分类
        BookCategory category = new BookCategory();
        BeanUtil.copyProperties(categoryDTO, category);
        
        // 保存分类
        this.save(category);
        return category.getCategoryId();
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateCategory(BookCategoryDTO categoryDTO) {
        // 检查分类是否存在
        BookCategory category = this.getById(categoryDTO.getCategoryId());
        if (category == null) {
            throw new BizException("分类不存在");
        }
        
        // 如果修改了分类名称，检查是否重复
        if (!categoryDTO.getCategoryName().equals(category.getCategoryName())) {
            LambdaQueryWrapper<BookCategory> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(BookCategory::getCategoryName, categoryDTO.getCategoryName());
            if (this.count(queryWrapper) > 0) {
                throw new BizException("分类名称已存在");
            }
        }
        
        // 更新分类
        BeanUtil.copyProperties(categoryDTO, category);
        return this.updateById(category);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteCategory(Integer categoryId) {
        // 检查分类是否存在
        BookCategory category = this.getById(categoryId);
        if (category == null) {
            throw new BizException("分类不存在");
        }
        
        // 检查分类下是否有图书
        LambdaQueryWrapper<Book> bookQueryWrapper = new LambdaQueryWrapper<>();
        bookQueryWrapper.eq(Book::getCategoryId, categoryId);
        if (bookMapper.selectCount(bookQueryWrapper) > 0) {
            throw new BizException("该分类下存在图书，无法删除");
        }
        
        // 删除分类
        return this.removeById(categoryId);
    }

    @Override
    public BookCategoryDTO getCategoryById(Integer categoryId) {
        // 查询分类
        BookCategory category = this.getById(categoryId);
        if (category == null) {
            throw new BizException("分类不存在");
        }
        
        // 转换为DTO
        return convertToDTO(category);
    }

    @Override
    public List<BookCategoryDTO> getAllCategories() {
        // 查询所有分类
        List<BookCategory> categories = this.list();
        
        // 转换为DTO
        return categories.stream()
                .map(this::convertToDTO)
                .collect(Collectors.toList());
    }
    
    /**
     * 将BookCategory实体转换为BookCategoryDTO
     * @param category 分类实体
     * @return 分类DTO
     */
    private BookCategoryDTO convertToDTO(BookCategory category) {
        if (category == null) {
            return null;
        }
        BookCategoryDTO categoryDTO = new BookCategoryDTO();
        BeanUtil.copyProperties(category, categoryDTO);
        return categoryDTO;
    }
}